package com.lzx.iteam.task;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;
import com.lzx.iteam.util.ImageUtil;
import com.lzx.iteam.util.PhotoUtil;
import com.lzx.iteam.util.StringUtil;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * Created by zhangshuai on 15-5-14.
 */
public class DownloadImageTask extends AsyncTask<String, Void, Drawable> {

    ImageView mImageView;
    String mUrl;
    Activity mContext;
    boolean mIsSave;

    public DownloadImageTask(ImageView imageView) {
        mImageView = imageView;
    }

    public DownloadImageTask(Activity activity,boolean sava) {
        this.mContext = activity;
        this.mIsSave = sava;
    }

    public DownloadImageTask(ImageView imageView, String url) {
        mImageView = imageView;
        mUrl = url;
    }

    protected Drawable doInBackground(String... urls) {
        return loadImageFromNetwork(urls[0]);
    }

    protected void onPostExecute(Drawable result) {

        BitmapDrawable bd = (BitmapDrawable) result;
        Bitmap bm = bd.getBitmap();
        if (result != null && mImageView != null) {
            if (!StringUtil.isEmpty(mUrl) ) {
                if (mImageView.getTag().equals(mUrl)){
                    mImageView.setImageBitmap(ImageUtil.createRoundBitmap(bm));
                }
            }else{
                mImageView.setImageBitmap(bm);
            }
        }else{
            if (mIsSave){
                savaImageToLocal(bm);
            }
        }

    }

    public static Drawable loadImageFromNetwork(String imageUrl) {
        Drawable drawable = null;
        try {
            // 可以在这里通过文件名来判断，是否本地有此图片
            drawable = Drawable.createFromStream(
                    new URL(imageUrl).openStream(), "image.jpg");
        } catch (Exception e) {
            //Log.d("test", e.getMessage());
        }
        if (drawable == null) {
            Log.d("test", "null drawable");
        } else {
            Log.d("test", "not null drawable");
        }
        return drawable;
    }

    /**
     * 保存图片到本地
     *
     * @param bimap
     */
    public void savaImageToLocal(Bitmap bimap) {
        String fileUri = null;
        try {
//                    fileUri = MediaStore.Images.Media.insertImage(getContentResolver(), mImagePath, System.currentTimeMillis() + "", "");
            fileUri = MediaStore.Images.Media.insertImage(mContext.getContentResolver(), bimap, System.currentTimeMillis() + "", "");
//                } catch (FileNotFoundException e) {
        } catch (Exception e) {
            e.printStackTrace();
        }
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        if (fileUri != null) {
            Uri geturi = Uri.parse(fileUri);
            String filepath = PhotoUtil.getFilePathByContentResolver(mContext, geturi);
            Uri sendnuri = Uri.fromFile(new File(filepath));
            intent.setData(sendnuri);
            mContext.sendBroadcast(intent);
            Toast.makeText(mContext, "保存成功", Toast.LENGTH_SHORT).show();
        }
    }
}
